package fr.upmc.i3s.fuglmeyeranalyser.view.menus;

import java.awt.GridLayout;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import fr.upmc.i3s.fuglmeyeranalyser.patients.FMScore;
import fr.upmc.i3s.fuglmeyeranalyser.patients.Patient;
import fr.upmc.i3s.fuglmeyeranalyser.patients.PatientList;
import fr.upmc.i3s.fuglmeyeranalyser.patients.PatientParameter;
import fr.upmc.i3s.fuglmeyeranalyser.tools.xls.XLSDataFileContent;
import fr.upmc.i3s.fuglmeyeranalyser.view.strings.StringsConstants;

public class MenuDataIn extends JPanel  {

	private static final long serialVersionUID = -5852480518070583350L;
	
	private Object[][] datas;

	/**
	 * Create the panel.
	 */
	public MenuDataIn(XLSDataFileContent patientListFromXLSFile) {
		super(new GridLayout(0, 1));
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		JTable table = createTable(patientListFromXLSFile);
		JScrollPane scrollpane = new JScrollPane();
		scrollpane.setViewportView(table);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		add(scrollpane);
	}
	
	public JTable createTable(XLSDataFileContent patientListFromXLSFile) {
		int optionalParamsNb = patientListFromXLSFile.getParametersNumber();
		int scoreParamsNb = patientListFromXLSFile.getMaxPatientScoreNumber();
		int patientNb = patientListFromXLSFile.getPatientNumber();
		System.out.println("patientNb="+patientNb+", optionalParamsNb="+optionalParamsNb+", scoreParamsNb="+scoreParamsNb);
		
		// build entetes
		LinkedList<String> params = patientListFromXLSFile.getParamsList();
		String[] entetes = new String[optionalParamsNb+scoreParamsNb*2]; 
		int cpt = 0;
		for(String param : params) {
			entetes[cpt] = param;
			//System.out.println("param name = "+entetes[cpt]);
			cpt++;
		}
		for(cpt=optionalParamsNb; cpt<optionalParamsNb+scoreParamsNb*2; cpt=cpt+2){
			entetes[cpt] = StringsConstants.ROW_NAME_DELAY;
			//System.out.println("param name = "+entetes[cpt]);
			entetes[cpt+1] = StringsConstants.ROW_NAME_SCORE;
			//System.out.println("param name = "+entetes[cpt+1]);
		}
		
		// build data matrix
		datas = new Object[patientNb][optionalParamsNb+scoreParamsNb*2];
		int i = 0;
		PatientList patients = patientListFromXLSFile.getPatientList();
		for(Patient patient : patients) {
			cpt = 0;
			Object[] patientData = new Object[optionalParamsNb+scoreParamsNb*2];
			LinkedList<PatientParameter> patientParams = patient.getParamsList();
			for(PatientParameter patientParam : patientParams) {
				patientData[cpt] = patientParam.getValue();
				cpt++;
			}
			LinkedList<FMScore> scoreList = patient.getScoreList();
			for(FMScore score : scoreList){
				patientData[cpt] = score.getDelayAfterStroke();
				patientData[cpt+1] = score.getFuglMeyerScore();
				cpt+=2;
			}
			for(cpt=optionalParamsNb+patient.getScoreList().size()*2; cpt<optionalParamsNb+scoreParamsNb*2; cpt++) {
				patientData[cpt] = "-";
			}
			datas[i] = patientData;
			i++;
		}
		
		// build table
		JTable table = new JTable(datas, entetes);
		table.getTableHeader().setReorderingAllowed(false);
		return table;
	}
	
	public void setData(Object[][] donnees) {
		this.datas = donnees;
	}
	
	public Object[][] getData() {
		return datas;
	}

}
